package com.nic.view.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.nic.view.R;
import com.nic.view.model.CheckType;
import com.nic.view.model.CheckViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/11/9.
 */

public abstract class CommonCheckAdapter<K, H extends CheckViewHolder> extends CommonAdapter<K> {
    public final static int NORMAL = 0;
    public final static int REFRESH = 1;
    public final static int LOAD_MORE = 2;
    private int pageSize = 15;
    private boolean isLoadMore;

    public void setLoadMore(boolean loadMore) {
        isLoadMore = loadMore;
    }

    private View getLoadMoreView(Context context) {
        return LayoutInflater.from(context).inflate(R.layout.item_load_more, null);
    }

    private OnClickItemMultiListener listener;
    private OnClickItemSingleListener sListener;

    private CheckType checkType;
    private SparseArray<Boolean> checkMap = new SparseArray<>();

    public CheckType getCheckType() {
        return checkType;
    }


    @Override
    public void initDataPreview() {
        initCheckMap();
        super.initDataPreview();
    }

    public CommonCheckAdapter(CheckType checkType) {
        this.checkType = checkType;
    }

    public CommonCheckAdapter() {
        checkType = CheckType.NONE;
    }

    private void initCheckMap() {
    }

    @Override
    public void setList(List<K> list, boolean isUpDate) {
        reset();
        super.setList(list, isUpDate);
    }

    /**
     * 所有状态集合
     *
     * @return
     */
    public SparseArray<Boolean> getCheckMap() {
        return checkMap;
    }

    /**
     * 重置状态
     */
    public void reset() {
        checkMap.clear();
//        notifyDataSetChanged();
    }

    @Override
    public View getBindView(final int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
//        if (LOAD_MORE == type) {
//            return getLoadMoreView(parent.getContext());
//        }
        int key = getTag(type);
        H holder;
        if (convertView == null || convertView.getTag(key) == null) {
            holder = onCreateViewHolder(parent, type);
            initCheckBox(parent, holder.create());
            convertView = holder.getRootView();
            convertView.setTag(key, holder);
        } else {
            holder = (H) convertView.getTag(key);
        }
        checkBindViewState(position, holder.create());
        onBindViewHolder(parent, holder, position);
        return convertView;
    }

    protected int getTag(int itemViewType) {
        return R.id.view_def_checked;
    }


    /**
     * 初始化
     *
     * @param parent
     * @param viewType
     * @return
     */
    public abstract H onCreateViewHolder(ViewGroup parent, int viewType);

    /**
     * 绑定
     *
     * @param parent
     * @param holder
     * @param position
     */
    public abstract void onBindViewHolder(ViewGroup parent, H holder, int position);

    /**
     * 放入getBindItemView中监听checkBox状态
     *
     * @param position
     * @param checkBox
     */
    public void checkBindViewState(final int position, final CheckBox checkBox) {
        if (checkBox == null) {
            return;
        }
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCheckStateListener(position, checkBox, false);
            }
        });
        if (checkMap.get(position, false)) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

    }

    private void checkChange(boolean isChecked, int position) {
        try {
            if (CheckType.MULTI == getCheckType()) {
                checkMap.put(position, isChecked);
                if (listener != null) {
                    listener.checkedItem(getCheckedMap());
                }
            } else if (CheckType.SINGLE == getCheckType()) {
                setSelected(position);
                if (sListener != null) {
                    sListener.onItemClick(position);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param position
     * @param checkBox
     * @param isClickContent 是否点击内容区域而不是checkbox本身
     */
    public void setCheckStateListener(int position, CheckBox checkBox, boolean isClickContent) {
        if (isClickContent) {
            if (checkBox.isChecked()) {
                checkBox.setChecked(false);
            } else {
                checkBox.setChecked(true);
            }
        }
        checkChange(checkBox.isChecked(), position);
    }

    /**
     * checkBox初始化时设置checkBox属性
     *
     * @param parent
     * @param checkBox
     */
    public void initCheckBox(ViewGroup parent, CheckBox checkBox) {
        if (checkBox == null) {
            return;
        }
        if (checkType == CheckType.NONE) {
            checkBox.setVisibility(View.GONE);
        } else {
            if (checkType == CheckType.SINGLE) {
                checkBox.setButtonDrawable(R.drawable.k_selector_cb);//单选
            }
        }
    }

    public abstract String getItemBeanId(int position);

    public abstract String getItemBeanName(int position);

    /*全选*/
    public void selectedAll() {
        unSelected();
        for (int i = 0; i < getList().size(); i++) {
            checkMap.put(i, true);
        }
        notifyDataSetChanged();
    }


    /*反选*/
    public void unSelected() {
        for (int i = 0; i < getList().size(); i++) {
            checkMap.put(i, false);
        }
        notifyDataSetChanged();
    }

    /**
     * 获取选中项的id和name集合
     *
     * @return
     */
    public Map<String, String> getCheckedMap() {
        if (getList().isEmpty()) {
            return new HashMap<>();
        }

        Map<String, String> checkedMap = new HashMap<>();
        for (int i = 0; i < getList().size(); i++) {
            if (checkMap.get(i, false)) {
                checkedMap.put(getItemBeanId(i), getItemBeanName(i));
            }
        }
        return checkedMap;
    }

    /**
     * 获取选中项的List
     *
     * @return
     */
    public List<K> getCheckedList() {
        if (getList().isEmpty()) {
            return new ArrayList<>();
        }
        List<K> list = new ArrayList<>();
        for (int i = 0; i < getList().size(); i++) {
            if (checkMap.get(i, false)) {
                list.add(getList().get(i));
            }
        }
        return list;
    }

    /**
     * 设置已选状态
     *
     * @param map
     */
    public void setCheckMap(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return;
        }
        for (int i = 0; i < getList().size(); i++) {
            if (map.containsKey(getItemBeanId(i))) {
                checkMap.put(i, true);
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 单选状态
     */
    public void setSelected(int selectedPosition) {
//        initCheckMap();
        for (int i = 0; i < getList().size(); i++) {
            if (i == selectedPosition)
                checkMap.put(i, true);
            else
                checkMap.put(i, false);
        }
        notifyDataSetChanged();
    }

//    @Override
//    public int getItemViewType(int position) {
//        if (getList().size() % pageSize == 0&&isLoadMore) {
//            return LOAD_MORE;
//        }
//        return super.getItemViewType(position);
//    }
//
//    @Override
//    public int getCount() {
//        if (getList().size() % pageSize == 0&&isLoadMore) {
//            return super.getCount() + 1;
//        }
//        return super.getCount();
//    }

    public void setClickItemMultiListener(OnClickItemMultiListener listener) {
        this.listener = listener;
    }

    public void setClickItemSingleListener(OnClickItemSingleListener sListener) {
        this.sListener = sListener;
    }

    public interface OnClickItemMultiListener {
        void checkedItem(Map<String, String> map);
    }

    public interface OnClickItemSingleListener {
        void onItemClick(int position);
    }
}
